package com.example.springboot_zc.controller;

import com.example.springboot_zc.pojo.*;
import com.example.springboot_zc.service.classDisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/classDis")
public class classDisController {

    @Autowired
    classDisService classDisService;

    @GetMapping("/select")
    public List<classDis> getAll(){return classDisService.list();}

    @GetMapping("/find/{status}&{stuNum}")
    public boolean findStu(@PathVariable String status,@PathVariable String stuNum){
        return classDisService.updateSta(status,stuNum);
    }

    @GetMapping("/getAllClass")
    public Set<String> getAllClass(){
        return classDisService.getAllClass();
    }

    @PostMapping("/getNum/{status}&{classNum}")
    public Integer getNum(@PathVariable("classNum") String classNum, @PathVariable("status") String status){
        return classDisService.getStatusNum(classNum,status);
    }

    @PostMapping("/getN/{status}")
    public Integer getSN(@PathVariable("status") String status){
        return classDisService.getSN(status);
    }

    @PostMapping("/add/{name}")
    public void sp(@RequestBody Object o,@PathVariable String name) {

        String s = o.toString();
//        System.out.println(s);

        classDis classDis1=new classDis();
        int zyu=0,dyu=0,tyu=0,lyu=0,myu=0;
        int z1=0,z2=0,d1=0,d2=0,t1=0,t2=0,l1=0,l2=0,m1=0,m2=0;
        double zp=0.6,dp=0.2,tp=0.1,lp=0.05,mp=0.05;

        String[] str = s.split("\\{");
        int flag1 = -1, flag2 = -1, i = 0;
        int[] num = {0, 0, 0, 0, 0};
        for (String q : str) {
            if (flag1 < 1) {
                flag1++;
                continue;
            }
//            System.out.println(q);
            int y = q.indexOf('}');
            int a = q.substring(y + 1, q.length() - 1).indexOf("Data");
            num[i]++;
            if (a != -1) {
                i++;
            }
        }
        for (int j = 0; j < num.length; j++) {
            num[j] = num[j] * 10;
//            System.out.println(num[j]);
        }
        for (String q : str) {
            if (flag2 < 1) {
                flag2++;
                continue;
            }
            int y = q.indexOf('}');
            String p = q.substring(0, y + 1);
            p = '{' + p;
//            System.out.println(p);
            Map<String, String> m = selfController.mapStringToMap(p);
            Set<String> keys = m.keySet();
//            for(String key:keys){
//                System.out.println("key: "+key+" value: "+m.get(key));
//            }
            for (String key : keys) {
//                System.out.println("key："+key+" value:"+m.get(key));
                if (num[0] > 0) {
                    if (key.equals("zyBwScore"))
                        z1=Integer.valueOf(m.get(key));
                    if (key.equals("zySelfScore"))
                        z2=Integer.valueOf(m.get(key));
                    classDis1.setContent("2022-2023综合测评");
                    if(key.equals("stuNum"))
                        classDis1.setStuNum(m.get(key));
                    if(key.equals("classNum"))
                        classDis1.setClassNum(m.get(key));
                    num[0]--;
                    if(num[0] % 10 == 0)
                        zyu+=(z1+z2)/2;
                    continue;
                }

                if (num[1] > 0) {
                    if (key.equals("dyBwScore"))
                        d1=Integer.valueOf(m.get(key));
                    if (key.equals("dySelfScore"))
                        d2=Integer.valueOf(m.get(key));
                    classDis1.setContent("2022-2023综合测评");
                    if(key.equals("stuNum"))
                        classDis1.setStuNum(m.get(key));
                    if(key.equals("classNum"))
                        classDis1.setClassNum(m.get(key));
                    num[1]--;
                    if(num[1] % 10 == 0)
                        dyu+=(d1+d2)/2;
                    continue;
                }

                if (num[2] > 0) {
                    if (key.equals("tyBwScore"))
                        t1=Integer.valueOf(m.get(key));
                    if (key.equals("tySelfScore"))
                        t2=Integer.valueOf(m.get(key));
                    classDis1.setContent("2022-2023综合测评");
                    if(key.equals("stuNum"))
                        classDis1.setStuNum(m.get(key));
                    if(key.equals("classNum"))
                        classDis1.setClassNum(m.get(key));
                    num[2]--;
                    if(num[2] % 10 == 0)
                        tyu+=(t1+t2)/2;
                    continue;
                }

                if (num[3] > 0) {
                    if (key.equals("lyBwScore"))
                        l1=Integer.valueOf(m.get(key));
                    if (key.equals("lySelfScore"))
                        l2=Integer.valueOf(m.get(key));

                    classDis1.setContent("2022-2023综合测评");
                    if(key.equals("stuNum"))
                        classDis1.setStuNum(m.get(key));
                    if(key.equals("classNum"))
                        classDis1.setClassNum(m.get(key));
                    num[3]--;
                    if(num[3] % 10 == 0)
                        lyu+=(l1+l2)/2;
                    continue;
                }

                if (num[4] > 0) {
                    if (key.equals("myBwScore"))
                        m1=Integer.valueOf(m.get(key));
                    if (key.equals("mySelfScore"))
                        m2=Integer.valueOf(m.get(key));
                    classDis1.setContent("2022-2023综合测评");
                    if(key.equals("stuNum"))
                        classDis1.setStuNum(m.get(key));
                    if(key.equals("classNum"))
                        classDis1.setClassNum(m.get(key));
                    num[4]--;
                    if(num[4] % 10 == 0)
                        myu+=(m1+m2)/2;
                    continue;
                }
            }

        }
        //提交到classDis表
        classDis1.setName(name);
        classDis1.setZyScore((int)(zyu*zp));
        classDis1.setDyScore((int)(dyu*dp));
        classDis1.setTyScore((int)(tyu*tp));
        classDis1.setLyScore((int)(lyu*lp));
        classDis1.setMyScore((int)(myu*mp));
        classDis1.setZcScore((int)(zyu*zp+dyu*dp+tyu*tp+lyu*lp+myu*mp));
        classDis1.setClassRank(5);
        classDis1.setStatus("已审核");
        LocalDate date=LocalDate.now();
        String dat=date.toString().replace('-','.');
        classDis1.setShTime(dat);
        classDisService.deleteByStu(classDis1.getStuNum());
        classDisService.save(classDis1);
    }
}
